package com.linwen.excise.leetcode;

public class _206反转链表 {
    public static void main(String[] args) {
        ListNode node5 = new ListNode(5);
        ListNode node4 = new ListNode(4);
        ListNode node3 = new ListNode(3);
        ListNode node2 = new ListNode(2);
        ListNode node1 = new ListNode(1);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        ListNode reverseList = reverseList(node1);
        while (reverseList!=null){
            System.out.println(reverseList.val);
            reverseList = reverseList.next;
        }
    }

    public static ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode curr = head;
        ListNode prive = null;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prive;
            prive = curr;
            curr = next;
        }
        return prive;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

}
